package com.tosit.driverschool.controller;

import com.tosit.driverschool.common.entity.User;
import com.tosit.driverschool.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     *   初始界面
      * @param modelMap
     * @return
     */
    @GetMapping("/getAll")
    public String getAll(ModelMap modelMap){
        List<User> users = userService.findAll();
        modelMap.put("users",users);
        modelMap.put("content","user-list");
        return "index";
    }
    @GetMapping("/selectById")
    @ResponseBody
    public JSONObject CeShi(@RequestParam("id") String id) {
        System.out.println("进入");
        JSONObject jsonObject = new JSONObject();
        User user = this.userService.selectbyId(id);
        System.out.println(user.toString());
        System.out.println(user.getHiredate());

        Date a=user.getHiredate();
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd ");//多态
        //2017-04-19 星期三 下午 20:17:38
        String format = bf.format(a);//格式化 bf.format(date)
        System.out.println(format);
        if(user!=null) {
            jsonObject.put("id", user.getId());
            jsonObject.put("hiredate",format);
            jsonObject.put("name", user.getName());
            jsonObject.put("password", user.getPassword());
        }
        System.out.println(jsonObject.toString());
        return jsonObject;
    }
    @GetMapping("/deleteById/{id}")
    public String delete(ModelMap modelMap,@PathVariable("id") String id){
        System.out.println(id);
        userService.deleteById(id);
        return "redirect:/user/getAll";
    }
    @PostMapping("/add")
    public String add(User user){
        System.out.println(user.toString());
        userService.add(user);
        return "redirect:/user/getAll";
    }
    @PutMapping("/upDate")
    public  String upDate(User user){
        System.out.println(user.getHiredate());
        System.out.println(user.toString());
        userService.upDate(user);
        return "redirect:/user/getAll";
    }









}
